package com.actimize.dao.hibernate;

import java.util.List;

import javax.persistence.Table;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.orm.hibernate4.SessionFactoryUtils;
import org.springframework.stereotype.Repository;

import com.actimize.dao.AccountDao;
import com.actimize.model.Account;
import com.actimize.model.Role;
import com.actimize.model.User;

@Repository("accountDao")
public class AccountDaoHibernate extends GenericDaoHibernate<Account, Long>
		implements AccountDao {

	public AccountDaoHibernate() {
        super(Account.class);
        
    }
	
	@SuppressWarnings("unchecked")
	public List<Account> getAll() {
		List<Account> accounts = getSession().createCriteria(Account.class).list();
        if (accounts.isEmpty()) {
            return null;
        } else {
            return accounts;
        }
	}

	public Account save(Account object) {
		return this.saveAccount(object);
	}


	public Account saveAccount(Account account) {
		if (log.isDebugEnabled()) {
			log.debug("account's id: " + account.getAccountID());
		}
		getSession().saveOrUpdate(account);
		return account;

	}

	@SuppressWarnings("unchecked")
	public List<Account> getAccountByUserID(Long userID) {
		System.out.println("Dragan ima user id="+userID);
//		 List<Account> tmpList =  getHibernateTemplate().find("from Account where userID=?", userID);    
		List<Account> accounts = getSession().createCriteria(Account.class).add(Restrictions.eq("userID", userID)).list();
        if (accounts.isEmpty()) {
            return null;
        } else {
            return accounts;
        }
		 
	}
	public Account getAccountByAccountID(String accountId) {
		Query qry = getSession().createQuery("from Account u where accountID ="+ accountId);

		List accounts = qry.list();
				//getSession().createCriteria(Account.class).add(Restrictions.eq("accountID", accountId)).list();
        if (accounts.isEmpty()) {
            return null;
        } else {
            return (Account) accounts.get(0);
        }
    }
	public boolean updateAmount (String account, String sort, Double amount ){
		System.out.println(account);
		Account acnt = this.getAccountByAccountID(account);
		acnt.setBalance(acnt.getBalance()+amount);
		this.saveAccount(acnt);
		
		return true;
	}
}
